package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;
    public PageInfo list(StrategyCommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyComment> list = strategyCommentMapper.selectForList(qo);
        return new PageInfo<StrategyComment>(list);
    }

    @Override
    public void saveOrUpdate(StrategyComment strategyComment) {
        if (strategyComment.getId()!=null){
            strategyCommentMapper.updateByPrimaryKey(strategyComment);
        }else {
            strategyComment.setUser(UserContext.getCurrentUser());
            strategyComment.setCreateTime(new Date());
            strategyComment.setCommendTime(new Date());
            strategyComment.setState(0);
            strategyCommentMapper.insert(strategyComment);
        }
    }


    public void updateState(StrategyComment strategyComment) {
        //如果为推荐 则加个推荐时间
        if (strategyComment.getState() == 1) {
            strategyComment.setCommendTime(new Date());
        }
        strategyCommentMapper.updateState(strategyComment);
    }

    @Override
    public StrategyComment selectBystrategycommentId(Long strategycommentId) {
        return strategyCommentMapper.selectByPrimaryKey(strategycommentId);
    }

    public PageInfo queryListByAuthorId(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<StrategyComment> list = strategyCommentMapper.selectListByAuthorId(qo);
        return new PageInfo(list);
    }

    @Override
    public int queryForCount(Long authorId) {
        return strategyCommentMapper.selectForCount(authorId);
    }

    @Override
    public List<StrategyComment> selectByState() {
        return strategyCommentMapper.selectByState();
    }


}
